package kz.asoft;

import java.awt.Color;

import java.util.Calendar;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import kz.asoft.am.common.RisksAppModule;
import kz.asoft.utils.ADFUtils;

import oracle.adf.view.faces.bi.component.pivotTable.CellFormat;
import oracle.adf.view.faces.bi.component.pivotTable.DataCellContext;
import oracle.adf.view.faces.bi.component.pivotTable.UIPivotTable;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;

import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.bali.ewt.graphics.ColorUtils;


public class CoverCtrlObjManageBean {
    private RichSelectOneChoice yearFrom;
    private RichSelectOneChoice yearTo;
    private UIPivotTable pivotTable;

    public CoverCtrlObjManageBean() {
    }

    /**
     * Custom cell data format
     * @param dataCellContext
     * @return
     */
    public CellFormat getDataFormat(DataCellContext dataCellContext) {
        CellFormat cellFormat = new CellFormat(null, null, null);

        String color = null;
        Object value = dataCellContext.getValue();
        if (value != null && value instanceof Number) {
            int intVal = Integer.valueOf(value.toString());
            if (intVal == 0) {
                color =
                        "background-color:" + ColorUtils.colorToHTML(Color.RED) +
                        ";";
            } else if (intVal == 1) {
                color =
                        "background-color:" + ColorUtils.colorToHTML(Color.GREEN) +
                        ";";
            } else if (intVal == 2) {
                color =
                        "background-color:" + ColorUtils.colorToHTML(Color.YELLOW) +
                        ";";
            }
        }
        cellFormat.setStyle(color);
        cellFormat.setTextStyle("display: none;");
        return cellFormat;
    }

    /**
     * Анализ периодов охвата субъектов квазигосударственного сектора контрольными мероприятиями СК
     * @param actionEvent
     */
    public void getCoverCtrlObjReport(ActionEvent actionEvent) {
        getCoverCtrlObjReport();
    }
    
    /**
     * Custom value change listener
     * @param valueChangeEvent
     */
    public void valueChangeListener(ValueChangeEvent valueChangeEvent) {
        getCoverCtrlObjReport();
    }
    
    /**
     * Запрос анализа периодов охвата субъектов квазигосударственного сектора контрольными мероприятиями СК
     */
    private void getCoverCtrlObjReport(){
        RisksAppModule appModule =
            (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        int yearFromInt;
        int yearToInt;
        if (yearFrom != null && yearFrom.getValue() != null) {
            yearFromInt = Integer.valueOf(yearFrom.getValue().toString());
        } else {
            yearFromInt = Calendar.getInstance().get(Calendar.YEAR);
        }
        if (yearTo != null && yearTo.getValue() != null) {
            yearToInt = Integer.valueOf(yearTo.getValue().toString());
        } else {
            yearToInt = Calendar.getInstance().get(Calendar.YEAR);
        }
        appModule.getCoverCtrlObjReport(yearFromInt, yearToInt);
    }

    public void setYearFrom(RichSelectOneChoice yearFrom) {
        this.yearFrom = yearFrom;
        int year = Calendar.getInstance().get(Calendar.YEAR);
        yearFrom.setValue(year);
        AdfFacesContext.getCurrentInstance().addPartialTarget(this.yearFrom);
    }

    public RichSelectOneChoice getYearFrom() {
        return yearFrom;
    }

    public void setYearTo(RichSelectOneChoice yearTo) {
        this.yearTo = yearTo;
        int year = Calendar.getInstance().get(Calendar.YEAR);
        yearTo.setValue(year);
        AdfFacesContext.getCurrentInstance().addPartialTarget(this.yearTo);
    }

    public RichSelectOneChoice getYearTo() {
        return yearTo;
    }

    public void setPivotTable(UIPivotTable pivotTable) {
        this.pivotTable = pivotTable;
    }

    public UIPivotTable getPivotTable() {
        return pivotTable;
    }
}
